package com.solver.controller.admin;

import com.github.pagehelper.PageHelper;
import com.solver.dto.QuestionDTO;
import com.solver.dto.QuestionPageQueryDTO;
import com.solver.result.PageResult;
import com.solver.result.Result;
import com.solver.service.QuestionService;
import com.solver.vo.QuestionVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/admin/dish")
public class QuestionController {
    @Autowired
    private QuestionService questionService;

    /**
     * 新增问题功能
     * @param questionDTO
     * @return
     */
    @PostMapping
    public Result saveQuestion(@RequestBody QuestionDTO questionDTO){
        questionService.saveWithFlavor(questionDTO);
        return Result.success();
    }

    /**
     * 问题分页查询
     * @param questionPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    public Result questionPageQuery(QuestionPageQueryDTO questionPageQueryDTO){
        log.info("问题分页查询，questionPageQueryDTO:{}", questionPageQueryDTO);

        PageResult pageResult = questionService.questionPageQuery(questionPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 批量删除菜品
     * @param ids
     * @return
     */
    @DeleteMapping
    public Result deleteBatch(@RequestParam List<Long> ids){
        log.info("问题（批量）删除ids:{}", ids);
        questionService.deleteBatch(ids);
        return Result.success();
    }

    /**
     * 根据id查询问题
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<QuestionVO> getQuestionById(@PathVariable Long id){
        log.info("根据id查询问题，id：{}",id);
        QuestionVO questionVO = questionService.getQuestionById(id);
        log.info("问题查询结果，{}",questionVO);
//        questionVO.setCategoryName("特色蒸菜");
        return Result.success(questionVO);
    }

    /**
     * 修改问题数据以及关联的难度数据
     * @param questionDTO
     * @return
     */
    @PutMapping
    public Result updateQuestion(@RequestBody QuestionDTO questionDTO){
        log.info("修改问题questionDTO:{}", questionDTO);
        questionService.updateWithDifficulty(questionDTO);
        return Result.success();
    }

}
